package com.gjzt.business.media.service;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.media.inter.BusTypeMapper;
import com.gjzt.business.media.model.BusType;
import com.gjzt.business.utils.Constant;

@Service
public class BusTypeService extends BaseService {

    @Autowired
    private BusTypeMapper busTypeMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONArray findBusType(ReqBean reqBean) {

        SqlBuilder sqlBuilder = new SqlBuilder("select * from MEDIA_BUS_TYPE a where 1=1 ")
                .addWhereLike2("a.TYPE_NAME", reqBean.getInfo("quickSearchName")).orderBy(" order by a.TYPE_NAME ");
        return this.query(sqlBuilder);
    }


    /**
     * 新增
     *
     * @param reqBean
     */
    public void addBusType(ReqBean reqBean) {
        BusType busType = getBusType(reqBean);
        busType.setTypeId(intIncrementId("MEDIA_BUS_TYPE","TYPE_ID"));
        busType.setTypeCode(busType.getTypeName());
        busType.setStatus(Constant.DATA_STATUS_TRUE);
        busTypeMapper.insert(busType);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateBusType(ReqBean reqBean) {
        String typeId = reqBean.getInfo("typeId");
        if (StringUtil.isEmpty(typeId)) {
            throw new BusinessException("获取主键失败");
        }
        BusType busType = getBusType(reqBean);
        busType.setTypeCode(busType.getTypeName());
        busTypeMapper.updateByPrimaryKeySelective(busType);
    }

    /**
     * 失效
     *
     * @param reqBean
     */
    public void disableBusType(String typeId) {
        if (StringUtil.isEmpty(typeId)) {
            throw new BusinessException("获取主键失败");
        }
        busTypeMapper.updateStatusByPrimaryKey(typeId, Constant.DATA_STATUS_FALSE);
    }
    
    /**
     * 生效
     * @param typeId
     */
    public void revertBusType(String typeId) {
        if (StringUtil.isEmpty(typeId)) {
            throw new BusinessException("获取主键失败");
        }
        busTypeMapper.updateStatusByPrimaryKey(typeId, Constant.DATA_STATUS_TRUE);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private BusType getBusType(ReqBean reqBean) {
        BusType busType = new BusType();

        busType.setTypeId(reqBean.getInfo("typeId"));
        busType.setTypeCode(reqBean.getInfo("typeCode"));
        busType.setTypeName(reqBean.getInfo("typeName"));
        busType.setDescText(reqBean.getInfo("descText"));
        busType.setDescImg(reqBean.getInfo("descImg"));
        busType.setStatus(reqBean.getInfo("status"));
        return busType;
    }

}